package com.atguigu.day03;

import com.atguigu.bean.Event;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

public class Flink06_Transform_Map {
    public static void main(String[] args) throws Exception {
        //1.获取流的执行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        env.setParallelism(1);

        //2.从端口读取数据
        DataStreamSource<String> streamSource = env.socketTextStream("localhost", 9999);

        //TODO 3.将读进来的字符串转为Event
//        SingleOutputStreamOperator<Event> map = streamSource.map(new MapFunction<String, Event>() {
//            @Override
//            public Event map(String value) throws Exception {
//                String[] split = value.split(",");
//                return new Event(split[0], split[1], Long.parseLong(split[2]));
//            }
//        });
        SingleOutputStreamOperator<String> map = streamSource.map(new MapFunction<String, String>() {
            @Override
            public String map(String value) throws Exception {
                String[] split = value.split(",");
                Event event = new Event(split[0], split[1], Long.parseLong(split[2]));
                if ("songsong".equals(event.user)) {
                   return event.user;
                } else if ("bingbing".equals(event.user)) {
                    return event.user + ":" + event.url;
                }
                return "";
            }
        });

        map.print();

        env.execute();


    }
}
